package net.ib.mtalk.util;

import net.ib.mtalk.ui.start.AuthenticationActivity;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.widget.Toast;

/**
 * 백그라운드에서 SMS 문자 보내기를 위한 솔루션을 제공한다.
 * android.permission.SEND_SMS 권한 필요
 * @author Arngard
 * @since 2011.01.27.
 */
public class SmsMessager {
	
	private static BroadcastReceiver mSendReceiver;
	//private static BroadcastReceiver mDeliveredReceiver;
	
	/**
	 * 백그라운드 Self SMS를 위한 클래스. 폰의 기능을 이용하여 SMS를 보낸다.
	 * @param context
	 * @param phoneNumber
	 * @param messageText
	 */
	public static void send(final Context context, String phoneNumber, String messageText) {
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";
		PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0);
		PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);
		
        //---when the SMS has been sent---
		mSendReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
            	Toast.makeText(context, "SmsMessager send : getResultCode() = " + getResultCode(), Toast.LENGTH_SHORT).show();
                switch (getResultCode())
                {
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                    case Activity.RESULT_CANCELED:
                        ((AuthenticationActivity)context).requestServerSMS();
                        break;
                    case Activity.RESULT_OK:
                    default:
                    	break;
                }
                unregisterReceiver(arg0);
            }
        };
		context.registerReceiver(mSendReceiver, new IntentFilter(SENT));
 
		
		/*mDeliveredReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show();
                        break;                       
                }
                unregisterReceiver(arg0);
            }
        };*/
        //---when the SMS has been delivered---
		//context.registerReceiver(mDeliveredReceiver, new IntentFilter(DELIVERED));

		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, messageText, sentPI, deliveredPI);
	}
	
	private static void unregisterReceiver(Context context)
	{
		context.unregisterReceiver(mSendReceiver);
		//context.unregisterReceiver(mDeliveredReceiver);
	}
}
